********************************************************
*This file cleans employment at state level from Census*
********************************************************

*******************************
*			State			  *
*******************************

foreach year in 1970 1990 {

use if year==`year' using "$raw_data_lmarket/ipums_census.dta", clear

*Drop institutional group quarters* 
drop if gqtyped>=100 & gqtyped<=499
*Drop alaska and hawai*
drop if statefip==2 | statefip==15
*Restrict to working-age population who are salaried workers*
keep if age>=16 & age<=65 & empstat==1 & (classwkrd==22 | classwkrd==23 )
gen emppriv=1

**recode the IND1990 to IFR crosspath**
run "$project/xwalks/xwalks_industry/xpath_ind1990_ifr19.do"
tab ind1990 if industry_ifr==""
drop if industry_ifr==""

**Merge czones using geography xwalk** 
*Needed because statefips variable is not complete for 1970. Therefore assign CZs to their state of 1990
if `year'==1970{
gen ctygrp1970=cntygp97
collapse (sum) emppriv [fw=perwt], by(industry_ifr ctygrp1970)  fast
joinby ctygrp1970 using "$project/xwalks/xwalks_geography/ctygrp1970_czone.dta", unmatched(master)
assert czone!=. 
}
else if `year'==1990{
gen puma1990=statefip*10000+puma
collapse (sum) emppriv [fw=perwt], by(industry_ifr puma1990)  fast
joinby puma1990 using "$project/xwalks/xwalks_geography/puma1990_czone.dta", unmatched(master)
assert czone!=. 
}

merge m:1 czone using "$list/czones_list.dta", nogenerate
merge m:1 statefip using "$list/states_list.dta", keep(1 3) nogenerate

**Aggregate at the czone level**
collapse (sum) emppriv [iw=afac], by(industry_ifr statefip) fast

save "$clean_data_lmarket/state_`year'_emp_by_ifr19.dta", replace

}

*******************************
*	   State, exclude CZ	  *
*******************************

foreach year in 1970 1990 {

use "$list/czones_list.dta", clear
merge 1:m czone using "$clean_data_lmarket/czone_`year'_emp_by_ifr19.dta", assert(3) nogenerate

bysort statefip industry_ifr19: egen emp_state_industry = sum(emppriv)
gen emppriv_noCZ_shockState = emp_state_industry-emppriv
replace emppriv_noCZ_shockState = 0 if emppriv_noCZ_shockState<0

keep czone emppriv_noCZ_shockState industry_ifr19

save "$clean_data_lmarket/czone_`year'_emp_by_ifr19_noCZ_shockState.dta", replace

}



